﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETFK
{
    /// <summary>
    /// 这个类用来判断两个对象是否一致
    /// </summary>
    public class DataCompare
    {
        /// <summary>
        /// 判断对象是否一致
        /// </summary>
        /// <param name="s">源对象</param>
        /// <param name="d">目标对象</param>
        /// <returns>一致返回true,否则返回False</returns>
        public static bool Compare(object s, object d)
        {
            if (s == null || d == null)
            {
                if (s == null & d == null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            if (s.GetType().Equals(d.GetType()) == false)
            {
                return false;
            }

            if (Items.ContainsKey(s.GetType()) == true)
            {
                if (Items[s.GetType()].Compare(s, d) == false)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }

            if(s.Equals(d)==false)
            {
                return false;
            }

            return true;
        }


        private delegate bool DelegateDataCompare(object s, object d);
        private static Dictionary<Type, CompareProcess> _Items = new Dictionary<Type, CompareProcess>();

        private static Dictionary<Type, CompareProcess> Items
        {
            get 
            { 
                if(_Items.Count ==0)
                {
                    Init();
                }
                return _Items; 
            }
            set { _Items = value; }
        }

        private static void Init()
        {
            CompareProcess cpBase = new CompareProcess();
            cpBase.HandleDataCompare = new DelegateDataCompare(BaseTypeDataCompare);

            _Items.Add(typeof(int), cpBase);
            _Items.Add(typeof(Int16), cpBase);
            _Items.Add(typeof(Int64), cpBase);
            _Items.Add(typeof(IntPtr), cpBase);

            _Items.Add(typeof(Single), cpBase);

            _Items.Add(typeof(double), cpBase);
        }

        private static bool BaseTypeDataCompare(object s,object d)
        {
            return s.Equals(d);
        }

        private class CompareProcess
        {
            public DelegateDataCompare HandleDataCompare = null;

            public bool Compare(object s, object d)
            {
                if (HandleDataCompare != null)
                {
                    return HandleDataCompare(s, d);
                }
                else
                {
                    return s.Equals(d);
                }
            }
        }

    
    }
}
